package com.rdzn.mall.portal.service.impl;

import com.rdzn.mall.base.util.RedisKeyUtil;
import com.rdzn.mall.base.util.RedisUtil;
import com.rdzn.mall.center.entity.UmsMember;
import com.rdzn.mall.center.service.IUmsFootprintService;
import com.rdzn.mall.center.service.IUmsMemberService;
import com.rdzn.mall.center.service.IUmsProductCollectService;
import com.rdzn.mall.portal.domain.MemberDetails;
import com.rdzn.mall.portal.domain.UmsUserInfo;
import com.rdzn.mall.portal.service.ICouponService;
import com.rdzn.mall.portal.service.IUserService;
import com.rdzn.mall.portal.service.IUmsMemberCacheService;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUmsMemberService memberService;
    @Autowired
    private IUmsMemberCacheService memberCacheService;
    @Autowired
    private IUmsProductCollectService productCollectService;
    @Autowired
    private IUmsFootprintService footprintService;
    @Autowired
    private ICouponService couponService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public MemberDetails getUserDetails() {
        return new MemberDetails(this.getCurrentMember());
    }

    @Override
    public MemberDetails getUserById(Integer memberId) {
        return new MemberDetails(memberService.getById(memberId));
    }

    @Override
    public MemberDetails getUserDetails(String wxOpenId) {
        UmsMember umsMember = this.getMemberByWxOpenId(wxOpenId);
        return new MemberDetails(umsMember);
    }

    @Override
    public UmsMember getMemberByWxOpenId(String wxOpenId) {
        String key = redisUtil.getKeyWithColumn(RedisKeyUtil.MEMBER_INFO, wxOpenId);
        if (redisUtil.hasKey(key)){
            return (UmsMember)redisUtil.get(key);
        }
        UmsMember umsMember = memberService.getByWeXinOpenId(wxOpenId);
        redisUtil.set(key, umsMember);
        return umsMember;
    }

    @Override
    public UmsMember getCurrentMember() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        Object principal = auth.getPrincipal();
        if (principal instanceof MemberDetails){
            MemberDetails memberDetails = (MemberDetails) principal;
            return memberDetails.getUmsMember();
        }
        return null;
    }

    @Override
    public MemberDetails loginOrRegisterUser(UmsMember umsMember, String invitationCode) {
        UmsMember umsMember1 = this.getMemberByWxOpenId(umsMember.getWeixinOpenid());
        if (null == umsMember1) {
            if (StringUtils.isNotEmpty(invitationCode)){
                UmsMember invitor = memberService.getByInvitationCode(invitationCode);
                if (invitor != null){
                    umsMember.setParentUserId(invitor.getId());
                }
            }
            umsMember.setInvitationCode(UUID.randomUUID().toString().replaceAll("-","").toUpperCase());
            umsMember.setCreateTime(new Date());
            memberService.save(umsMember);
            return new MemberDetails(umsMember);
        }
        return new MemberDetails(umsMember1);
    }

    @Override
    public LoginUser getUserByName(String username) {
        return null;
    }

    @Override
    public void updateIntegration(Integer id, Integer integration) {
        memberService.updateIntegration(id, integration);
        memberCacheService.delMember(id);
    }

    @Override
    public UmsUserInfo getUmsUserInfo(Integer memberId) {
        UmsUserInfo info = new UmsUserInfo();
        info.setCollectNum(productCollectService.countByUserId(memberId));
        info.setFootNum(footprintService.countByUserId(memberId));
        info.setCouponNum(couponService.countByMemberId(memberId));
        return info;
    }
}
